package org.fly.jingwei.bean;

import cn.hutool.core.date.DateUtil;
import lombok.Data;

import java.io.Serializable;

/**
 * @author leo.jie (weixiao.me@aliyun.com)
 * @version 1.0
 * @organization bigdata
 * @website https://www.jielongping.com
 * @date 2020/5/31 1:55 下午
 * @since 1.0
 */
@Data
public class ResponseUtil<T> implements Serializable {
    private boolean success;
    private int code = 200;
    private String msg;
    private long timestamp = DateUtil.currentSeconds();
    private T payload;

    public ResponseUtil() {

    }

    public ResponseUtil(boolean success) {
        this.success = success;
    }

    public ResponseUtil(boolean success, T payload) {
        this.success = success;
        this.payload = payload;
    }

    public ResponseUtil<T> success(boolean success) {
        this.success = success;
        return this;
    }

    public ResponseUtil<T> payload(T payload) {
        this.payload = payload;
        return this;
    }

    public ResponseUtil<T> code(int code) {
        this.code = code;
        return this;
    }

    public ResponseUtil<T> message(String msg) {
        this.msg = msg;
        return this;
    }

    public static <T> ResponseUtil<T> ok() {
        return (new ResponseUtil<T>()).success(true);
    }

    public static <T> ResponseUtil<T> ok(T payload) {
        return (new ResponseUtil<T>()).success(true).payload(payload);
    }

    public static <T> ResponseUtil<T> ok(T payload, int code) {
        return (new ResponseUtil<T>()).success(true).payload(payload).code(code);
    }

    public static <T> ResponseUtil<T> failed() {
        return (new ResponseUtil<T>()).success(false);
    }

    public static <T> ResponseUtil<T> failed(T payload) {
        return (new ResponseUtil<T>()).success(false).payload(payload);
    }

    public static <T> ResponseUtil<T> failed(T payload, int code) {
        return (new ResponseUtil<T>()).success(false).payload(payload).code(code);
    }
}
